package com.atguigu.ucenterservice.service.impl;


import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.LoginVo;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.atguigu.ucenterservice.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;



@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
/*
1.验证是否为空
2.验证手机号是否重复
2.验证 验证码
3.md5加密密码
4.补充信息插入数据库
 */
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "注册信息错误");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new GuliException(20001, "该手机已注册");
        }
        String rPhone = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(rPhone)) {
            throw new GuliException(20001, "验证码错误");
        }
        String Md5Password = MD5.encrypt(password);
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setPassword(Md5Password);
        ucenterMember.setMobile(mobile);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/jJHyeM0EN2jhB70LntI3k8fEKe7W6CwykrKMgDJM4VZqCpcxibVibX397p0vmbKURGkLS4jxjGB0GpZfxCicgt07w/132");
        ucenterMember.setNickname(nickname);
        ucenterMember.setIsDisabled(false);
        baseMapper.insert(ucenterMember);


    }

    @Override
    public String login(LoginVo loginVo) {
        /*
        1.获取参数验空
        2.根据手机拿出用户信息
        3.验证密码是否正确
        4.生成token
         */
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录信息有误");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        if (ucenterMember == null) {
            throw new GuliException(20001, "用户或密码错误");
        }
        String UCPassword = ucenterMember.getPassword();
        String Md5Password = MD5.encrypt(password);
        if (!UCPassword.equals(Md5Password)) {
            throw new GuliException(20001, "用户或密码错误");
        }
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    @Override
    public Integer countRegister(String day) {
        Integer count=baseMapper.countRegister(day);
        return count;
    }
}
